package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 集合的排序
 */
public class SortPoint {
    public static void main(String[] args) {
        List<PointCol>list=new ArrayList<>();
        list.add(new PointCol(5,8));
        list.add(new PointCol(15,60));
        list.add(new PointCol(57,89));
        list.add(new PointCol(1,4));
        list.add(new PointCol(10,8));
        list.add(new PointCol(22,35));
        System.out.println("list原始数据 = " + list);
        System.out.println("*******************************");

        //自定义排序规则：
        //jdk1.8时，List集合自身提供了sort方法进行排序
        list.sort(new Comparator<PointCol>() {
            @Override
            public int compare(PointCol o1, PointCol o2) {
                return o2.getX()-o1.getX();
            }
        });
        System.out.println("list排序 = " + list);










        Collections.sort(list, (o1, o2)-> {
                int len1=o1.getX()+o1.getY();
                int len2=o2.getX()+o2.getY();
                return len1-len2;
        });
        System.out.println("list排序后 = " + list);
    }
}
